package sam.msm.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Ordering;

import sam.msm.dto.ResultDTO;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiListingReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
	@Bean
	public Docket createRestApi() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).forCodeGeneration(true)
				.useDefaultResponseMessages(false).select()
				.apis(RequestHandlerSelectors.basePackage("sam.msm.api.controller")).build()
				.apiDescriptionOrdering(new Ordering<ApiDescription>() {
					@Override
					public int compare(ApiDescription left, ApiDescription right) {
						int position = left.getDescription().compareTo(right.getDescription());
						return position;
					}
				}).apiListingReferenceOrdering(new Ordering<ApiListingReference>() {
					@Override
					public int compare(ApiListingReference left, ApiListingReference right) {
						int position = left.getDescription().compareTo(right.getDescription());
						return position;
					}
				});
		return docket;
	}
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("三颗树项目")
				.description("\n\r-\n\r*所有接口中的非必传参数，不传则代表不使用该个参数做查询或写入" + "-\n\r-\n\r*返回值标准格式"
						+ JSONObject.toJSONString(new ResultDTO(), true))
				.version("1.0").termsOfServiceUrl("http://api.www.{env}.5km.com/").build();
	}
}
